package com.heima.common.rabbitmq;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Configuration;

/**
 * MQ的配置
 *
 * 配置完后并不会生效 需要去 hm-common/src/main/resources/META-INF/spring.factories 配置才会生效
 *
 */
@Configuration
@ConditionalOnClass({RabbitTemplate.class})//只有含有RabbitTemplate依赖的才会生效
public class MqConfig {

    /**
     * MQ的JSON消息转换器
     */
    @Bean
    public MessageConverter MessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * MQ工具类
     * @param rabbitTemplate
     * @return
     */
    @Bean
    public RabbitMqHelper rabbitMqHelper(RabbitTemplate rabbitTemplate){
        return new RabbitMqHelper(rabbitTemplate);
    }
}